package com.lagou.utils;

import com.lagou.annotation.MyAutowire;
import com.lagou.annotation.MyComponent;

import java.sql.SQLException;

@MyComponent("transactionManager")
public class TransactionManager {

    @MyAutowire("connectionUtil")
    private static ConnectionUtil connectionUtil;

    //事务开启
    public static void begin(){
        try {
            connectionUtil.getConnection().setAutoCommit(false);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    //事务提交
    public static void commit(){
        try {
            connectionUtil.getConnection().commit();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    //事务回滚
    public static void rollback(){
        try {
            connectionUtil.getConnection().rollback();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
